package com.graduation.sports.ui.widget;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.graduation.sports.R;
import com.graduation.sports.base.BaseDialogFragment;
import com.graduation.sports.bean.SportPlanBean;
import com.graduation.sports.config.Session;
import com.graduation.sports.listener.OnAddPlanListener;
import com.graduation.sports.util.DateUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: sports
 * @Package: com.graduation.sports.ui.widget
 * @ClassName: AddPlanDialog
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/3/3 15:49
 * @Version: 1.0
 */
public class AddPlanDialog extends BaseDialogFragment {

    @BindView(R.id.et_start_date)
    EditText etStartDate;
    @BindView(R.id.et_end_date)
    EditText etEndDate;
    @BindView(R.id.et_sport_name)
    TextView etSportName;
    @BindView(R.id.layout_distance)
    LinearLayout layoutDistance;
    @BindView(R.id.layout_times)
    LinearLayout layoutTimes;
    @BindView(R.id.et_distance)
    EditText etDistance;
    @BindView(R.id.et_times)
    EditText etTimes;
    @BindView(R.id.et_train_time)
    EditText etTrainTime;

    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;


    private String sportName = "";

    private OnAddPlanListener mListener;

    @Override
    protected int getLayout() {
        return R.layout.dialog_add_plan;
    }

    @Override
    protected void initView(View view) {

    }

    @OnClick({R.id.et_sport_name, R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_sport_name:
                showSportSelectDialog();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                savePlan();
                break;
        }
    }

    @Override
    protected void parseData(Bundle arguments) {

    }

    private void savePlan(){
        String startDate = etStartDate.getText().toString().trim();
        String endDate = etStartDate.getText().toString().trim();
        sportName = etSportName.getText().toString().trim();
        String distance = etDistance.getText().toString().trim();
        String times = etTimes.getText().toString().trim();
        String trainTime = etTrainTime.getText().toString().trim();
        if (TextUtils.isEmpty(startDate)) {
            ToastUtils.showShort("请输入开始日期");
            return;
        }
        if (TextUtils.isEmpty(endDate)) {
            ToastUtils.showShort("请输入结束日期");
            return;
        }
        if (TextUtils.isEmpty(sportName)) {
            ToastUtils.showShort("请选择锻炼科目");
            return;
        }
        if ("跑步".equals(sportName) && TextUtils.isEmpty(distance)) {
            ToastUtils.showShort("请输入跑步目标距离");
            return;
        }
        if (!"跑步".equals(sportName) && TextUtils.isEmpty(times)) {
            ToastUtils.showShort("请输入锻炼目标次数");
            return;
        }
        if (TextUtils.isEmpty(trainTime)) {
            ToastUtils.showShort("请输入锻炼时长");
            return;
        }

        SportPlanBean planBean = new SportPlanBean();
        planBean.setStartDate(startDate);
        planBean.setEndDate(endDate);
        planBean.setSportName(sportName);
        planBean.setTargetDistance(Double.parseDouble(distance));
        planBean.setTargetTimes(Integer.parseInt(times));
        planBean.setTrainTime(trainTime);
        planBean.setCreateDate(DateUtils.getOldDate(0));
        if (planBean.save()){
            ToastUtils.showShort("添加成功");
            dismiss();
        } else  {
            ToastUtils.showShort("添加失败");
            return;
        }


    }


    private void showSportSelectDialog(){
        int rid;
        if (Session.getUserInfo().getGrade() < 4) {
            rid = R.array.lowSportNames;
        } else {
            rid = R.array.highSportNames;
        }
        new XPopup.Builder(getContext())
                .isDestroyOnDismiss(true)
                .asCenterList("选择锻炼科目", getContext().getResources().getStringArray(rid), (position, text) -> {
                    etSportName.setText(text);
                    sportName = text;
                    refreshUI();
                }).show();
    }

    private void refreshUI() {
        if ("跑步".equals(sportName)) {
            layoutDistance.setVisibility(View.VISIBLE);
            layoutTimes.setVisibility(View.GONE);
        } else {
            layoutDistance.setVisibility(View.GONE);
            layoutTimes.setVisibility(View.VISIBLE);
        }

    }


}
